package org.dream.ui.importui;

import java.util.List;

import org.dream.domain.Daydeal;
import org.dream.persistence.DayDealDAO;
import org.dream.persistence.DaydealPersistenceImpl;
import org.dream.service.ImportThread;
import org.dream.util.Log;
import org.dream.util.LogFactory;

public class ImportDaydealPanel extends ImportPanel {
	private boolean firstFlag;
	private List<Daydeal> holdDaydeals;
	private DayDealDAO dayDealDAO = new DaydealPersistenceImpl();

	public ImportDaydealPanel() {
		super();
		firstFlag = persistenceService.isEmpty("Daydeal");
	}

	@Override
	public void onClickImportButton() {
		if (firstFlag) {
			/** 如果是初始化导入 */
			result.setText("开始导入:");
			LogFactory.getLog().registerProcessView(Log.IMPORTE_DDAYDEALS_PROCESS, result);
			ImportThread importThread = new ImportThread(fileText.getText());
			importThread.start();
		} else {
			/** 如果是补充导入 */
			if (null != holdDaydeals && holdDaydeals.size() > 0) {
				persistenceService.saveDaydeals(holdDaydeals);
				result.setText("导入成功");
				holdDaydeals = null;
			}

		}

	}

	@Override
	protected void addPromptInfo() {
		if (firstFlag) {
			result.setText("已选择文件" + fileText.getText());
		} else {
			holdDaydeals = importService.getDaydealListByFiles(fileText.getText());
			result.setText("有" + (holdDaydeals == null ? 0 : holdDaydeals.size()) + "条数据要导入");
		}

	}

	@Override
	public void clearDb() {
		dayDealDAO.clearDaydeals();
		result.setText("清库成功");
	}

	@Override
	public String getTitle() {
		return firstFlag ? "初始导入行情信息" : "补充导入行情信息";

	}
}
